<?php

/**
 * Created by PhpStorm.
 * User: dev
 * Date: 5/9/14
 * Time: 3:18 PM
 * TABLE : comments
 * id
 * post_id
 * name
 * email
 * comments
 * created_on
 * updated_on
 * status
 */
class Comments extends Eloquent {

    public static $table = 'comments';
    public static $key = 'id';
    public static $timestamps = true;


    function __construct(){
        parent::__construct();

    }

    public function post()
    {
        return $this->belongs_to('Post');
    }

    public static function total(){
        if(Cache::get("comments_total")){
            return Cache::get("comments_total");
        }
        $count = Comments::count();
        Cache::put("comments_total",$count,60*24);
        return $count;
    }
    
    public static function rattig($postID=0){

        if(Cache::get("comments_rattig_".$postID)){
            return Cache::get("comments_rattig_".$postID);
        }
        $comments = Comments::where('post_id','=',$postID)->avg('ratting');
    	return $comments;
    }
    
    public static function getModule($variable = null){
        if(Cache::get("module_".$variable)){
            return Cache::get("module_".$variable);
        }
    	if (!empty($variable)) {
    		$value = DB::table('modules')
    		->where('variable', '=', trim($variable))
    		->where('status', '=', 51)
    		->only('module');
            Cache::put("module_".$variable, $value, 60 * 24 * 30);
    		return $value;
    	} else {
    		return "Invalid...";
    	}
    }
    
    
}